package org.eclipse.stp.policy_builder.model.source.wizards.ui;

import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.stp.policy_builder.Activator;
import org.eclipse.stp.policy_builder.model.source.SourceExtensionParser;
import org.eclipse.stp.policy_builder.model.source.SourceType;
import org.eclipse.stp.policy_builder.model.source.wizards.SourceContentProvider;
import org.eclipse.stp.policy_builder.model.source.wizards.SourceLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class SourceTypesSelectPage extends WizardPage{
	private final static String COL_LABEL_NAME = "Source" ;
	
	private final static String COL_LABEL_DESCRIPT = "Description" ;
	
	private CheckboxTableViewer checkboxTableViewer;
	public SourceTypesSelectPage(){
		super("selectInput");
		setTitle("Select a Source Type :");
		setDescription("Select a one of the following registered Source Types to get Message Sources");
	}
	@Override
	public void createControl(Composite parent) {
		// TODO Auto-generated method stub
		  Composite container = new Composite(parent, SWT.NULL);
	      container.setLayout(new FormLayout());
	      setControl(container);
	      //System.out.println("first page created...");
	      checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER);
	      checkboxTableViewer.addSelectionChangedListener(new ISelectionChangedListener(){
	    	@Override
			public void selectionChanged(SelectionChangedEvent event) {
				// TODO Auto-generated method stub
				updatePageButtons();
			}
	    	  
	      });
	      
	      
	      final Table table = checkboxTableViewer.getTable();
	      final FormData formData = new FormData();
	      formData.bottom = new FormAttachment(100, 0);
	      formData.right = new FormAttachment(100, 0);
	      formData.top = new FormAttachment(0, 0);
	      formData.left = new FormAttachment(0, 0);
	      table.setLayoutData(formData);
	      table.setHeaderVisible(true);

	      final TableColumn tableColumn = new TableColumn(table, SWT.NONE);
	      tableColumn.setWidth(200);
	      tableColumn.setText(COL_LABEL_NAME);

	      final TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
	      tableColumn_1.setWidth(250);
	      tableColumn_1.setText(COL_LABEL_DESCRIPT);
	      
	      
	      checkboxTableViewer.setContentProvider(new SourceContentProvider());
	      checkboxTableViewer.setLabelProvider(new SourceLabelProvider());
	      checkboxTableViewer.setInput(new SourceExtensionParser<SourceType>(Activator.PLUGIN_ID,"source"));
	}
	
	public void setVisible(boolean visible){
	
		if(visible){
			//checkboxTableViewer.setInput(new SourceExtensionParser<SourceType>(Activator.PLUGIN_ID,"source"));
		}
		super.setVisible(visible);
		
	}
	
	public SourceType getSelection(){
		Object[] checked = checkboxTableViewer.getCheckedElements();
	    int count = checked.length;
	    
	    if(count>0)
	    	return (SourceType)checked[0];
	    	
		return null ;
	}

    public boolean canFlipToNextPage() {
    	if(checkboxTableViewer.getCheckedElements().length==1){
    		return super.canFlipToNextPage();
    	}
    	return false;
    }
    
    public void updatePageButtons(){
    	getWizard().getContainer().updateButtons();
    }
}
